package org.trade.coupon.mq.processor;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.trade.common.constant.TradeEnums;
import org.trade.common.protocol.coupon.ChangeCoponStatusReq;
import org.trade.common.protocol.coupon.ChangeCoponStatusRes;
import org.trade.common.protocol.mq.CancelOrderMQ;
import org.trade.common.rocketmq.IMessageProcessor;
import org.trade.coupon.service.ICouponService;

import com.alibaba.fastjson.JSON;


public class CancelOrderProcessor implements IMessageProcessor{
    public static final Logger  LOGGER=LoggerFactory.getLogger(CancelOrderProcessor.class);
	@Autowired
	private ICouponService couponService;

	public boolean handleMessage(MessageExt messageExt) {
		try {
			String body=new String (messageExt.getBody(),"UTF-8");
		    String msgId=messageExt.getMsgId();
		    String tags=messageExt.getTags();
		    String keys=messageExt.getKeys();
		    LOGGER.info("user CancelOrderProcessor receive message:"+messageExt);
		    CancelOrderMQ cancelOrderMQ=JSON.parseObject(body,CancelOrderMQ.class);
		    if(StringUtils.isNotBlank(cancelOrderMQ.getCouponId())){
		    	ChangeCoponStatusReq changeCoponStatusReq=new ChangeCoponStatusReq();
		    	changeCoponStatusReq.setOrderId(cancelOrderMQ.getOrderId());
		    	changeCoponStatusReq.setCouponId(cancelOrderMQ.getCouponId());
		    	changeCoponStatusReq.setIsUsed(TradeEnums.YesNoEnum.NO.getCode());
		    	ChangeCoponStatusRes changeCoponStatusRes=this.couponService.changeCoponStatus(changeCoponStatusReq);
		    	
		    }
		    return true;
		} catch (Exception e) {
			return false;
		}
		
	}

}
